﻿from pycad.system import *
from pycad.runtime import *
from parts.utils import *

from NFox.Expression.DataSystem import *
from NFox.Expression.Runtime import *
from NFox.Expression.SymbolValues import *
from NFox.Geometry import *

#初始化实体转换器
Entity2D.EntityProvider = AcadEntityProvider()

@command("Tls:InsertPart", acrx.CommandFlags.Redraw)
def InsertPart():
    res = ed.GetString('\n请输入元件集名或标准号:')
    global _lastIndex
    if res.Status == aced.PromptStatus.OK:
        name = res.StringResult
        res = ed.GetString('\n请输入元件名:')
        if res.Status == aced.PromptStatus.OK:
            key = res.StringResult
            _lastIndex = SymbolIndex(name, key)
    ipart = Application.WorkingDatabase.ImagePartTable[_lastIndex]
    if ipart:
        with dbtrans() as tr:
            btr = acdb.BlockTableRecord()
            btr.Name = '*u'
            bid = tr.addrecord(tr.BlockTable, btr)
            bref = acdb.BlockReference(acge.Point3d.Origin, bid)
            btr = tr.opencurrspace()
            tr.addentity(btr, bref)
            tr.addregapp(RegAppName)
            SetData(bref, ipart, None)
            if tr.dragblock(bref) and ipart.HasParams:
                ed.SetImpliedSelection((bref.ObjectId))
            #ed.Document.SendStringToExecute('Tls:ShowPartPropertyList\n', False, False, False)
